﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Model;
using Portal.Utility;
using Portal.Web.Model;
using Portal.Core;
using dtSearch32Lib;
using DocumentSearchServiceModel;
using DocumentSearch32Lib;
using Portal.Systems.Model;
using Portal.Workflow.Bussiness;
using Portal.Cms.Utility;
using System.Data;

namespace Portal.Cms.Render
{
    public class SelectWorkflowForAddCmsDataRenderBll
    {
        static string GetPlugInJavascript(string SiteId)
        {
            string ScriptText =
                   "<script language=javascript>\r\n" +
                   
                   "</script>\r\n";
            return ScriptText;
        }


        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string GroupDataId = (string)WebConfig.GetConfigFromXml(OSiteParam,"NewsGroupDataForAdd");
                //SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, GroupDataId, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                string Html =
                   GetPlugInJavascript(SiteId) +
                   DrawSelectForm(SiteId, GroupDataId).HtmlContent;
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            
            return OAjaxOut;
        }
        public static string BaseSiteUrl
        {
            get
            {
                HttpContext context = HttpContext.Current;
                string baseUrl = context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/');
                return baseUrl;
            }
        }

        public static string ConvertUrl(string Url)
        {
            Url = Url.Replace("192.168.1.2/", BaseSiteUrl + "/");
            Url = Url.Replace("117.3.4.207/", BaseSiteUrl + "/");
            return Url;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawSelectForm(string SiteId, string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);

                RoleWorkingWorkflowCls[]
                    RoleWorkingWorkflows = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAllowAccessRoleWorkingWorkflow(SiteId, WebSessionUtility.GetCurrentLoginUser().RoleId);
                string Html =
                    "<div class=CssTip>Bạn có quyền đăng bài theo những quy trình dưới đây. Xin vui lòng chọn quy trình để đăng bài viết của bạn</div>\r\n";
                if (RoleWorkingWorkflows.Length == 0)
                {
                    Html += "<div class=CssNoData>Không có quyền viết tin bài cho bất kỳ quy trình nào</div>\r\n";
                }
                else
                {
                    SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                    string ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam)+"/workflow.png";
                    Html +=
                        "<div class=CssWorkflowForm>" +
                        "   <table>\r\n";
                    for (int iIndex = 0; iIndex < RoleWorkingWorkflows.Length; iIndex++)
                    {
                        string Icon = ButtonUrl + "/workflow.png";
                        Html += " <tr>\r\n" +
                                "   <td style=\"width:20px;\"><img class=CssIcon src=\""+ButtonUrl+"\"></td>\r\n" +
                                "   <td><a href=\"AddCmsData.aspx?gid=" + GroupDataId + "&dtl=" + RoleWorkingWorkflows[iIndex].DataLanguage + "&wfc=" + RoleWorkingWorkflows[iIndex].frkWorkflowControlId + "\">" + RoleWorkingWorkflows[iIndex].Subject + "</a></td>\r\n" +
                                " </tr>\r\n";
                    }
                        Html+=
                        "   </table>\r\n" +
                        "</div>\r\n";
                }

                

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


    }
}
